home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Textures SDK / common / scriplib.c < prev    next >
C/C++ Source or Header  |  1998-12-02  |  5KB  |  269 lines

  1. /***
  2. *
  3. *    Copyright (c) 1998, Valve LLC. All rights reserved.
  4. *    
  5. *    This product contains software technology licensed from Id 
  6. *    Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
  7. *    All Rights Reserved.
  8. *
  9. ****/
  10.  
  11. // scriplib.c
  12.  
  13. #include "cmdlib.h"
  14. #include "scriplib.h"
  15.  
  16. /*
  17. =============================================================================
  18.  
  19.                         PARSING STUFF
  20.  
  21. =============================================================================
  22. */
  23.  
  24. typedef struct
  25. {
  26.     char    filename[1024];
  27.     char    *buffer,*script_p,*end_p;
  28.     int     line;
  29. } script_t;
  30.  
  31. #define    MAX_INCLUDES    8
  32. script_t    scriptstack[MAX_INCLUDES];
  33. script_t    *script;
  34. int            scriptline;
  35.  
  36. char    token[MAXTOKEN];
  37. qboolean endofscript;
  38. qboolean tokenready;                     // only true if UnGetToken was just called
  39.  
  40. /*
  41. ==============
  42. AddScriptToStack
  43. ==============
  44. */
  45. void AddScriptToStack (char *filename)
  46. {
  47.     int            size;
  48.  
  49.     script++;
  50.     if (script == &scriptstack[MAX_INCLUDES])
  51.         Error ("script file exceeded MAX_INCLUDES");
  52.     strcpy (script->filename, ExpandPath (filename) );
  53.  
  54.     size = LoadFile (script->filename, (void **)&script->buffer);
  55.  
  56.     printf ("entering %s\n", script->filename);
  57.  
  58.     script->line = 1;
  59.  
  60.     script->script_p = script->buffer;
  61.     script->end_p = script->buffer + size;
  62. }
  63.  
  64.  
  65. /*
  66. ==============
  67. LoadScriptFile
  68. ==============
  69. */
  70. void LoadScriptFile (char *filename)
  71. {
  72.     script = scriptstack;
  73.     AddScriptToStack (filename);
  74.  
  75.     endofscript = false;
  76.     tokenready = false;
  77. }
  78.  
  79.  
  80. /*
  81. ==============
  82. ParseFromMemory
  83. ==============
  84. */
  85. void ParseFromMemory (char *buffer, int size)
  86. {
  87.     script = scriptstack;
  88.     script++;
  89.     if (script == &scriptstack[MAX_INCLUDES])
  90.         Error ("script file exceeded MAX_INCLUDES");
  91.     strcpy (script->filename, "memory buffer" );
  92.  
  93.     script->buffer = buffer;
  94.     script->line = 1;
  95.     script->script_p = script->buffer;
  96.     script->end_p = script->buffer + size;
  97.  
  98.     endofscript = false;
  99.     tokenready = false;
  100. }
  101.  
  102.  
  103. /*
  104. ==============
  105. UnGetToken
  106.  
  107. Signals that the current token was not used, and should be reported
  108. for the next GetToken.  Note that
  109.  
  110. GetToken (true);
  111. UnGetToken ();
  112. GetToken (false);
  113.  
  114. could cross a line boundary.
  115. ==============
  116. */
  117. void UnGetToken (void)
  118. {
  119.     tokenready = true;
  120. }
  121.  
  122.  
  123. qboolean EndOfScript (qboolean crossline)
  124. {
  125.     if (!crossline)
  126.         Error ("Line %i is incomplete\n",scriptline);
  127.  
  128.     if (!strcmp (script->filename, "memory buffer"))
  129.     {
  130.         endofscript = true;
  131.         return false;
  132.     }
  133.  
  134.     free (script->buffer);
  135.     if (script == scriptstack+1)
  136.     {
  137.         endofscript = true;
  138.         return false;
  139.     }
  140.     script--;
  141.     scriptline = script->line;
  142.     printf ("returning to %s\n", script->filename);
  143.     return GetToken (crossline);
  144. }
  145.  
  146. /*
  147. ==============
  148. GetToken
  149. ==============
  150. */
  151. qboolean GetToken (qboolean crossline)
  152. {
  153.     char    *token_p;
  154.  
  155.     if (tokenready)                         // is a token allready waiting?
  156.     {
  157.         tokenready = false;
  158.         return true;
  159.     }
  160.  
  161.     if (script->script_p >= script->end_p)
  162.         return EndOfScript (crossline);
  163.  
  164. //
  165. // skip space
  166. //
  167. skipspace:
  168.     while (*script->script_p <= 32)
  169.     {
  170.         if (script->script_p >= script->end_p)
  171.             return EndOfScript (crossline);
  172.         if (*script->script_p++ == '\n')
  173.         {
  174.             if (!crossline)
  175.                 Error ("Line %i is incomplete\n",scriptline);
  176.             scriptline = script->line++;
  177.         }
  178.     }
  179.  
  180.     if (script->script_p >= script->end_p)
  181.         return EndOfScript (crossline);
  182.  
  183.     if (*script->script_p == ';' || *script->script_p == '#' ||         // semicolon and # is comment field
  184.         (*script->script_p == '/' && *((script->script_p)+1) == '/')) // also make // a comment field
  185.     {                                            
  186.         if (!crossline)
  187.             Error ("Line %i is incomplete\n",scriptline);
  188.         while (*script->script_p++ != '\n')
  189.             if (script->script_p >= script->end_p)
  190.                 return EndOfScript (crossline);
  191.         goto skipspace;
  192.     }
  193.  
  194. //
  195. // copy token
  196. //
  197.     token_p = token;
  198.  
  199.     if (*script->script_p == '"')
  200.     {
  201.         // quoted token
  202.         script->script_p++;
  203.         while (*script->script_p != '"')
  204.         {
  205.             *token_p++ = *script->script_p++;
  206.             if (script->script_p == script->end_p)
  207.                 break;
  208.             if (token_p == &token[MAXTOKEN])
  209.                 Error ("Token too large on line %i\n",scriptline);
  210.         }
  211.         script->script_p++;
  212.     }
  213.     else    // regular token
  214.     while ( *script->script_p > 32 && *script->script_p != ';')
  215.     {
  216.         *token_p++ = *script->script_p++;
  217.         if (script->script_p == script->end_p)
  218.             break;
  219.         if (token_p == &token[MAXTOKEN])
  220.             Error ("Token too large on line %i\n",scriptline);
  221.     }
  222.  
  223.     *token_p = 0;
  224.  
  225.     if (!strcmp (token, "$include"))
  226.     {
  227.         GetToken (false);
  228.         AddScriptToStack (token);
  229.         return GetToken (crossline);
  230.     }
  231.  
  232.     return true;
  233. }
  234.  
  235.  
  236. /*
  237. ==============
  238. TokenAvailable
  239.  
  240. Returns true if there is another token on the line
  241. ==============
  242. */
  243. qboolean TokenAvailable (void)
  244. {
  245.     char    *search_p;
  246.  
  247.     search_p = script->script_p;
  248.  
  249.     if (search_p >= script->end_p)
  250.         return false;
  251.  
  252.     while ( *search_p <= 32)
  253.     {
  254.         if (*search_p == '\n')
  255.             return false;
  256.         search_p++;
  257.         if (search_p == script->end_p)
  258.             return false;
  259.  
  260.     }
  261.  
  262.     if (*search_p == ';')
  263.         return false;
  264.  
  265.     return true;
  266. }
  267.  
  268.  
  269.